#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>

pthread_t p_keycode[5];
#define PTHREAD_NAME(num)	keycode_##num##_pthread

void * function(void * arg)
{
	while (1)
	{
		printf("\t[thread one!]---------------------------\n");
		sleep(2);
	}
	//printf("\t[thread one!]---------------------------\n");
}

void * function2(void * arg)
{
	char ch = getchar();

	//printf("ch ------------------ : %c\n", ch);
	while(1) {
		printf("[thread two!]===========================\n");
		sleep(3);
	}
}

void thread_1()
{
	int err;
	void *ptr;

	err = pthread_create(&p_keycode[0], NULL, function, NULL);
	if (err == 0)
        printf("thread one created.\n");
	pthread_join(p_keycode[0], &ptr);
}

void thread_2()
{
	int err;
	void *ptr;

	err = pthread_create(&p_keycode[1], NULL, function2, NULL);
	if (err == 0)
        printf("thread two created.\n");
	pthread_join(p_keycode[1], &ptr);
}

void thread_create(void)
{
        int temp;
        memset(&p_keycode, 0, sizeof(p_keycode));          

        if((temp = pthread_create(&p_keycode[0], NULL, function, NULL)) != 0)      
                printf("thread one fail!\n");
		
        if((temp = pthread_create(&p_keycode[1], NULL, function2, NULL)) != 0)
                printf("thread two fail!\n");
}

void thread_wait(void)
{
        if(p_keycode[0] !=0) {                   
			pthread_join(p_keycode[0],NULL);
			//printf("thread one exit\n");
        }
        if(p_keycode[1] !=0) {               
			pthread_join(p_keycode[1],NULL);
           // printf("thread two exit\n");
        }
}

int main()
{
	//thread_1();
	//thread_2();
	thread_create();
	thread_wait();
}

